import {neon} from "@neondatabase/serverless"
import "dotenv/config.js"

// Create a SQL connection using DB URL
export const sql = neon(process.env.DATABASE_URL);

export async function initDB() {
    try {
        await sql`
            create table if not exists transactions(
            id serial primary key,
            user_id varchar(255) not null,
            title varchar(255) not null,
            amount decimal(10, 2) not null,
            category varchar(255) not null,
            created_at date not null default current_date
        )`
        console.log("database initialized successfully")
    } catch(error) {
        console.log("Error initialized DB", error)
        process.exit(1); // status code 1 meas failure, 0 success
    }
}
